// ====================Sprite_CardButton====================

/**
 * 按钮类
 */
class Sprite_CardButton extends Sprite_Clickable {
    initialize(x, y, bitmap, fun) {
        super.initialize();
        this._clickHandler = null;
        this._coldFrame = null;
        this._hotFrame = null;
        this._state = null;
        this.setButtonBitmap(bitmap);
        this.setupFrames();
        this.move(x, y);
        this.setClickHandler(fun);
    }

    setupFrames() {
        const w = this.width;
        const h = this.height / 2;
        this.setColdFrame(0, 0, w, h);
        this.setHotFrame(0, h, w, h);
        this.updateFrame();
        this.updateOpacity();
    }

    setButtonBitmap(bitmap) {
        if (typeof (bitmap) === "string") {
            this.bitmap = new Bitmap(200, 120);
            this.bitmap.fillRoundRect(0, 0, 200, 60, ColorManager.textColor(9));
            this.bitmap.fillRoundRect(0, 60, 200, 60, ColorManager.textColor(8));
            this.bitmap.fontSize = 45;
            this.bitmap.drawText(bitmap, 0, 0, 200, 60, "center");
            this.bitmap.drawText(bitmap, 0, 60, 200, 60, "center");
        } else {
            this.bitmap = bitmap;
        }
    }

    update() {
        Sprite_Clickable.prototype.update.call(this);
        this.updateFrame();
        this.updateOpacity();
        this.processTouch();
    }

    updateFrame() {
        const frame = this.isPressed() || this._state === "select" ? this._hotFrame : this._coldFrame;
        if (frame) {
            this.setFrame(frame.x, frame.y, frame.width, frame.height);
        }
    }

    updateOpacity() {
        this.opacity = this._pressed ? 255 : 192;
    }

    setClickHandler(method) {
        this._clickHandler = method;
    }

    setColdFrame(x, y, width, height) {
        this._coldFrame = new Rectangle(x, y, width, height);
    }

    setHotFrame(x, y, width, height) {
        this._hotFrame = new Rectangle(x, y, width, height);
    }

    onClick() {
        if (this._clickHandler) {
            this._clickHandler();
        } else {
            Input.virtualClick(this._buttonType);
        }
    }
}